//
//  GetCodeViewController.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/28.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class GetCodeViewController: BaseViewController {

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        initBaseLayout()
        layoutPageSubviews()
        configUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        textField.becomeFirstResponder()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - event response
    func agreeProtocolAction(sender: UIButton) {
        if !sender.isSelected {
            sender.isSelected = true
        } else {
            sender.isSelected = false
        }
    }
    
    func getCodeAction(sender: UIButton) {
        let fillOutCodeVC = FillOutCodeViewController()
        fillOutCodeVC.phoneNum = self.textField.text
        self.navigationController?.pushViewController(fillOutCodeVC, animated: true)
    }
    
    override func leftItemAction(sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        textField.resignFirstResponder()
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.view.addSubview(textField)
        self.view.addSubview(selectedBtn)
        self.view.addSubview(agreeLabel)
        self.view.addSubview(protocolBtn)
        self.view.addSubview(getCodeBtn)
    }
    
    func layoutPageSubviews() {
        textField.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(0)
            make.top.equalTo(self.view).offset(48)
            make.right.equalTo(self.view).offset(0)
            make.height.equalTo(48)
        }
        
        selectedBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(6)
            make.top.equalTo(textField.snp.bottom).offset(13)
            make.width.height.equalTo(32)
        }
        
        agreeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(selectedBtn.snp.right).offset(1)
            make.top.equalTo(textField.snp.bottom).offset(21)
            make.width.equalTo(28)
            make.height.equalTo(14)
        }
        
        protocolBtn.snp.makeConstraints { (make) in
            make.left.equalTo(agreeLabel.snp.right).offset(6)
            make.top.equalTo(textField.snp.bottom).offset(21)
            make.width.equalTo(135)
            make.height.equalTo(14)
        }
        
        getCodeBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(14)
            make.top.equalTo(protocolBtn.snp.bottom).offset(33)
            make.right.equalTo(self.view).offset(-14)
            make.height.equalTo(44)
        }
    }
    
    func configUI() {
        self.view.backgroundColor = UIColor(hexString: "#EEEEEE")
        self.title = "获取验证码"
        initNavItem(controller: self, imageName: "nav_delete_n", highlitedImageName: "nav_delete_h", selectedImageName: "", isLeft: true)
        initNavItem(controller: self, imageName: "nav_rule_n", highlitedImageName: "nav_rule_h", selectedImageName: "", isLeft: false)
    }
    
    
    // MARK: - setters and getters
    var _textField: CustomTextField!
    var textField: CustomTextField {
        if _textField == nil {
            _textField = CustomTextField()
            _textField.configUI(type: .GETCODE)
            _textField.delegate = self
        }
        return _textField
    }
    
    var _selectedBtn: UIButton!
    var selectedBtn: UIButton {
        if _selectedBtn == nil {
            _selectedBtn = UIButton()
            _selectedBtn.setBackgroundImage(UIImage(named: "icon_select_n"), for: .normal)
            _selectedBtn.setBackgroundImage(UIImage(named: "icon_select_h"), for: .highlighted)
            _selectedBtn.setBackgroundImage(UIImage(named: "icon_select_s"), for: .selected)
            _selectedBtn.isSelected = true
            _selectedBtn.addTarget(self, action: #selector(GetCodeViewController.agreeProtocolAction(sender:)), for: .touchUpInside)
        }
        return _selectedBtn
    }
    
    var _agreeLabel: UILabel!
    var agreeLabel: UILabel {
        if _agreeLabel == nil {
            _agreeLabel = UILabel()
            _agreeLabel.font = UIFont(name: "PingFangSC-Regular", size: 13)
            _agreeLabel.textColor = UIColor(hexString: "#999999")
            _agreeLabel.text = "同意"
        }
        return _agreeLabel
    }
    
    var _protocolBtn: UIButton!
    var protocolBtn: UIButton {
        if _protocolBtn == nil {
            _protocolBtn = UIButton()
            _protocolBtn.setTitleColor(UIColor(hexString: "#000000"), for: .normal)
            _protocolBtn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 13)
            _protocolBtn.setTitle("《HARMAY注册协议》", for: .normal)
        }
        return _protocolBtn
    }
    
    var _getCodeBtn: UIButton!
    var getCodeBtn: UIButton {
        if _getCodeBtn == nil {
            _getCodeBtn = UIButton()
            _getCodeBtn.backgroundColor = UIColor(hexString: "#C5C5C5")
            _getCodeBtn.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
            _getCodeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
            _getCodeBtn.setTitle("获取验证码", for: .normal)
            _getCodeBtn.isEnabled = false
            _getCodeBtn.addTarget(self, action: #selector(GetCodeViewController.getCodeAction(sender:)), for: .touchUpInside)
        }
        return _getCodeBtn
    }
}

extension GetCodeViewController: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if (textField.text?.characters.count)! >= 10 && string != "" {
            getCodeBtn.backgroundColor = UIColor(hexString: "#252525")
            getCodeBtn.isEnabled = true
        } else if (textField.text?.characters.count)! < 12 {
            getCodeBtn.backgroundColor = UIColor(hexString: "#C5C5C5")
            getCodeBtn.isEnabled = false
        }
        
        if textField.text?.characters.count == 11 && string != "" {
            return false
        }
    
        if !string.isNumber() {
            if string == "" {
                return true
            }
            return false
        }
        return true
    }
    
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        getCodeBtn.backgroundColor = UIColor(hexString: "#C5C5C5")
        getCodeBtn.isEnabled = false
        return true
    }
}
